// index.js
// 获取应用实例
const app = getApp()
Page({
  data: {
    shop:[],
    count:'1'
  },
  onLoad() {
    let _this=this
    wx.getUserInfo({
      success: function(res){
        //点击允许后获取微信昵称与微信头像
        var nickName=res.userInfo.nickName;
        var log=res.userInfo.avatarUrl;
        wx.login({
          success:function(e){
            //请求成功后获取你的code值
            var code=e.code;
            wx.request({
              //请求后台
              url: 'http://www.week3.com/index.php/loginInfo',
              //传code、nickName、log
              data: {code:code,nickName:nickName,log:log},
              success: function(arr){
                console.log(arr)
                //判断后台请求成功后
                if(arr.data.code==200){
                  //将返回的ID值存入缓存中
                  wx.setStorageSync('id', arr.data.id)
                }
              }
            })
          }
        })
      }
    })
    wx.request({
      url: 'http://www.week3.com/index.php/shopList',
      success(res){
        console.log(res.data.data)
        _this.setData({shop:res.data.data})
      }
    })
  },
  onReachBottom(){
    this.setData({count:this.data.count*1+1})
    let _this=this
    setTimeout(function () {
      wx.request({
        url: 'http://www.week3.com/index.php/shopList',
        data:{num:_this.data.count},
        success(res){
          console.log(res)
          if(res.data.data!='已经到底了'){
            _this.setData({shop:res.data.data})
          }else{
            wx.showToast({
              title: res.data.data,
              icon: 'error'
            })
          }
        }
      })
      wx.hideLoading()
    }, 2000)
  },
  shopInfo(res){
    let id=res.target.dataset.id
    wx.navigateTo({
      url: '/pages/activity/activity?id='+id,
    })
  }
})
